package com.yunc.modules.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.R;
import com.yunc.modules.cms.entity.CmsCategory;
import com.yunc.modules.cms.service.ICmsCategoryService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章分类
 *
 * @author congcong tao
 * @date 2018/6/21 17:09
 */
@RestController
@RequestMapping("cmsCategory")
public class CmsCategoryController extends CommonController {

    private static final Logger logger = LogManager.getLogger(CmsCategoryController.class);

    @Autowired
    private ICmsCategoryService cmsCategoryService;

    @GetMapping("/page")
    public Page<CmsCategory> page(Page<CmsCategory> page, CmsCategory cmsCategory){
        cmsCategoryService.page(page, new QueryWrapper<>(cmsCategory));
        return page;
    }

    @GetMapping("/list")
    public R list(CmsCategory cmsCategory){
        QueryWrapper<CmsCategory> ew = new QueryWrapper<CmsCategory>()
                .orderByAsc("order_num");
        List<CmsCategory> sysDeptList = cmsCategoryService.list(ew);
        return R.ok().put("data", sysDeptList);
    }

    @GetMapping("/queryCategoryList")
    public List<CmsCategory> queryCategoryList(CmsCategory cmsCategory){
        List<CmsCategory> cmsCategoryList = cmsCategoryService.queryCategoryList(cmsCategory);
        return cmsCategoryList;
    }

    @GetMapping("/queryCategoryListR")
    public R queryCategoryListR(CmsCategory cmsCategory){
        List<CmsCategory> cmsCategoryList = cmsCategoryService.queryCategoryList(cmsCategory);
        return R.ok().put("cmsCategoryList", cmsCategoryList);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        CmsCategory category = cmsCategoryService.getById(id);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CmsCategory category){
        return cmsCategoryService.saveCategory(category);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody CmsCategory category){
        return cmsCategoryService.updateCategory(category);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{menuId}")
    public R delete(@PathVariable String id){
        boolean flag = cmsCategoryService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
